using System.Collections.Generic;

namespace Pasta.Medio.Domain
{
	public class EnumsRepository : Repository<Enum>
	{
		private Dictionary<int, string> _values = new Dictionary<int, string>();
		private Dictionary<string, int> _keys = new Dictionary<string, int>();

		public EnumsRepository()
		{
			RefreshDictionary();
		}

		private void RefreshDictionary()
		{
			_values.Clear();
			_keys.Clear();
			foreach (var e in GetAll())
			{
				_values.Add(e.EnumId, e.Name);
				_keys.Add(e.Name,e.EnumId);
			}
		}

		public string GetName(int? id, bool refresh = true)
		{
			string name = null;
			if (id.HasValue && !_values.TryGetValue(id.Value, out name) && refresh)
			{
				RefreshDictionary();
				name = GetName(id, false);
			}
			return name ?? "-";
		}

		public int GetEnum(string name, bool refresh = true)
		{
			int id = 0;
			if (!_keys.TryGetValue(name, out id) && refresh)
			{
				RefreshDictionary();
				id = GetEnum(name, false);
			}
			return id;
		}
	}
}
